﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CpuSimulator
{
    public class Computer
    {
        public Cpu Cpu { get; set; }
        public Memory Ram { get; set; }
        public Memory Rom { get; set; }
        public HardDisk HardDisk { get; set; }
        public Display Display { get; set; }

        public Computer()
        {
            Ram = new Memory(0x400,"RAM");
            Rom = new Memory(0x64, "ROM");
            Dictionary<(int start, int end), Device> addressMap = new Dictionary<(int start, int end),Device>();
            addressMap.Add((0x0000, 0x0064), Rom);
            addressMap.Add((0x0064, 0x464), Ram);
            Cpu = new Cpu(addressMap);
            HardDisk = new HardDisk();
            Display = new Display();
        }

        private void WriteRom(int romstart)
        {
            List<UInt32> cmds = new List<uint>();
            cmds.Add(0b00000110010000000000000011100111); //jalr x1,x0,0x64

            int index = 0;
            foreach (var item in cmds)
            {
                byte b1 = (byte)item;
                byte b2 = (byte)(item >> 8);
                byte b3 = (byte)(item >> 16);
                byte b4 = (byte)(item >> 24);

                Rom[index++] = b1;
                Rom[index++] = b2;
                Rom[index++] = b3;
                Rom[index++] = b4;
            }
        }

        public void Start()
        {
            Ram.Clear();
            Rom.Clear();
            Cpu.Start();
            WriteRom(0);
        }

        public void Reset()
        {
            Cpu.Reset();
            Ram.Clear();
            Rom.Clear();
            WriteRom(0);
        }
    }
}
